<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

    <#if baseResultMap>
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
        <#list table.fields as field>
        <#if field.keyFlag><#--生成主键排在第一位-->
            <id column="${field.name}" property="${field.propertyName}"/>
        </#if>
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
            <result column="${field.name}" property="${field.propertyName}"/>
        </#list>
        <#list table.fields as field>
        <#if !field.keyFlag><#--生成普通字段 -->
            <result column="${field.name}" property="${field.propertyName}"/>
        </#if>
        </#list>
        </resultMap>

    </#if>
    <#if baseColumnList>
        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
            <#list table.commonFields as field>
                ${field.columnName},
            </#list>
            ${table.fieldNames}
        </sql>
    </#if>

    <#assign prefix = '#' + '{'/>
    <#assign suffix = '}'/>
    <#assign primaryKeyName = ''>
    <#assign primaryKeyPropertyName= ''>
    <#assign primaryKeyParameterType = ''>
    <#assign isContainLogicDeleted = false>
    <#assign lastFieldName = ''>
    <#assign lastFieldPropertyName = ''>
    <#assign index=1>
    <#assign size= table.fields?size>
    <#list table.fields as field>
    <#if field.keyFlag>
        <#assign primaryKeyName = field.name>
        <#assign primaryKeyPropertyName = field.propertyName>
        <#assign primaryKeyParameterType = field.propertyType>
    <#else>
        <#if field.name = logicDeleteFieldName>
            <#assign isContainLogicDeleted = true>
        </#if>
    </#if>
    <#if index = size>
        <#assign lastFieldName = field.name>
        <#assign lastFieldPropertyName = field.propertyName>
    </#if>
    <#assign index = index + 1>
    </#list>

    <insert id="insert" parameterType="${package.Entity}.${entity}">
        insert into ${table.name}
        (${table.fieldNames})
        values
        (
        <#list table.fields as field>
        <#if field.name = lastFieldName>
            ${prefix}${field.propertyName}${suffix}
        <#else>
            ${prefix}${field.propertyName}${suffix},
        </#if>
        </#list>
        )
    </insert>

    <insert id="insertSelective" parameterType="${package.Entity}.${entity}">
        insert into ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list table.fields as field>
            <if test="${field.propertyName} != null">
                ${field.name},
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list table.fields as field>
            <if test="${field.propertyName} != null">
                ${prefix}${field.propertyName}${suffix},
            </if>
        </#list>
        </trim>
    </insert>

    <insert id="insertUseGeneratedKeys" parameterType="${package.Entity}.${entity}" useGeneratedKeys="true" keyProperty="id">
        insert into ${table.name}
        (${table.fieldNames})
        values
        (
        <#list table.fields as field>
            <#if field.name = lastFieldName>
            ${prefix}${field.propertyName}${suffix}
        <#else>
            ${prefix}${field.propertyName}${suffix},
        </#if>
        </#list>
        )
    </insert>

    <insert id="insertBatch" parameterType="${package.Entity}.${entity}">
        insert into ${table.name}
        (${table.fieldNames})
        values
        <foreach collection="records" item="record" index="index" separator=",">
        (
        <#list table.fields as field>
        <#if field.name = lastFieldName>
            ${prefix}record.${field.propertyName}${suffix}
        <#else>
            ${prefix}record.${field.propertyName}${suffix},
        </#if>
        </#list>
        )
        </foreach>
    </insert>

    <update id="updateByPrimaryKey" parameterType="${package.Entity}.${entity}">
        update ${table.name} set
        <#list table.fields as field>
        <#if field.keyFlag>
        <#elseif field.name = lastFieldName>
        ${field.name} = ${prefix}${field.propertyName}${suffix}
        <#else>
        ${field.name} = ${prefix}${field.propertyName}${suffix},
        </#if>
        </#list>
        where ${primaryKeyName} = ${prefix}${primaryKeyPropertyName}${suffix}
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="${package.Entity}.${entity}">
        update ${table.name}
        <set>
        <#list table.fields as field>
        <#if field.keyFlag>
        <#else>
            <if test="${field.propertyName} != null">
                ${field.name} = ${prefix}${field.propertyName}${suffix},
            </if>
        </#if>
        </#list>
        </set>
        where ${primaryKeyName} = ${prefix}${primaryKeyPropertyName}${suffix}
    </update>

    <select id="selectByPrimaryKey" parameterType="java.lang.${primaryKeyParameterType}" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name}
        where ${primaryKeyName} = ${prefix}${primaryKeyPropertyName}${suffix}
    </select>

    <select id="selectByPrimaryKeys" parameterType="java.lang.${primaryKeyParameterType}" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name}
        where ${primaryKeyName} in
        <foreach collection="ids" item="id" open="(" close=")" index="index" separator=",">
            ${prefix}id${suffix}
        </foreach>
    </select>

    <select id="selectOneByRecord" parameterType="${package.Entity}.${entity}" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name}
        <where>
            <#list table.fields as field>
            <if test="${field.propertyName} != null">
                and ${field.name} = ${prefix}${field.propertyName}${suffix}
            </if>
            </#list>
        </where>
    </select>

    <select id="selectByRecord" parameterType="${package.Entity}.${entity}" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name}
        <where>
            <#list table.fields as field>
                <if test="${field.propertyName} != null">
                    and ${field.name} = ${prefix}${field.propertyName}${suffix}
                </if>
            </#list>
        </where>
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.${primaryKeyParameterType}">
        delete from ${table.name}
        where ${primaryKeyName} = ${prefix}${primaryKeyPropertyName}${suffix}
    </delete>

    <delete id="deleteByPrimaryKeys" parameterType="java.lang.${primaryKeyParameterType}">
        delete from ${table.name}
        where ${primaryKeyName} in
        <foreach collection="ids" item="id" open="(" close=")" index="index" separator=",">
            ${prefix}id${suffix}
        </foreach>
    </delete>

    <#if isContainLogicDeleted>
    <update id="logicDeleteByPrimaryKey" parameterType="java.lang.${primaryKeyParameterType}">
        update ${table.name} set
        ${logicDeleteFieldName} = 1
        where ${primaryKeyName} = ${prefix}${primaryKeyPropertyName}${suffix}
    </update>

    <update id="logicDeleteByPrimaryKeys" parameterType="java.lang.${primaryKeyParameterType}">
        update ${table.name} set
        ${logicDeleteFieldName} = 1
        where ${primaryKeyName} in
        <foreach collection="ids" item="id" open="(" close=")" index="index" separator=",">
            ${prefix}id${suffix}
        </foreach>
    </update>
    </#if>
</mapper>
